package com.xsy.carRental.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xsy.carRental.dto.CustomerAddDTO;
import com.xsy.carRental.dto.CustomerPageQueryDTO;
import com.xsy.carRental.dto.CustomerSearchQueryDTO;
import com.xsy.carRental.entity.Customer;
import com.xsy.carRental.mapper.CustomerMapper;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {


    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 用户注册
     *
     * @param customerAddDTO
     */
    public void add(CustomerAddDTO customerAddDTO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerAddDTO, customer);
        customer.setCreatedAt(LocalDateTime.now());
        customerMapper.add(customer);
    }

    /**
     * 分页查询用户
     *
     * @param customerPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CustomerPageQueryDTO customerPageQueryDTO) {
        PageHelper.startPage(customerPageQueryDTO.getPage(), customerPageQueryDTO.getPageSize());
        Page<Customer> page = customerMapper.pageQuery(customerPageQueryDTO);

        long total = page.getTotal();
        List<Customer> vehicles = page.getResult();
        return new PageResult(total, vehicles);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     */
    public void deleteById(int id) {
        customerMapper.deleteById(id);
    }

    @Override
    public PageResult SearchQuery(CustomerSearchQueryDTO searchQueryDTO) {

        PageHelper.startPage(searchQueryDTO.getPage(), searchQueryDTO.getPageSize());
        Page<Customer> page = customerMapper.SearchQuery(searchQueryDTO);
        long total = page.getTotal();
        List<Customer> vehicles = page.getResult();
        return new PageResult(total, vehicles);
    }
}
